require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_EmoticonFilters_test < Test::Unit::TestCase
   
    def startup
      $TEXT ='text'
      $TextUp= 'textupdate'
      @@ie = Watir::IE.start($SITE_URL)
     
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end    
      
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToSiteAdministration
        ie.goto($SITE_URL + 'controlpanel/Settings/default.aspx')
      
        assert(ie.contains_text('Site Administration'))      
    end
    
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Setup').exists?)
        assert(ie.link(:text, 'Manage Licenses').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
    def test_10_navigateToManageUrlRedirectsSettings
        ie.goto($SITE_URL + 'controlpanel/tools/EmoticonFilter.aspx')
        assert(ie.text_field(:id,/NewSmileyCode$/).exists?)
        ie.text_field(:id,/NewSmileyCode$/).value = $TEXT
        assert(ie.text_field(:id,/NewSmileyUrl$/).exists?)
        ie.text_field(:id,/NewSmileyUrl$/).value = 'http://www.windweaver.com/emoticon.htm#ANGRY'
        assert(ie.text_field(:id,/NewSmileyText$/).exists?)
        ie.text_field(:id,/NewSmileyText$/).value = 'Angry '
        assert(ie.button(:id,/Create$/).exists?)
        ie.button(:id,/Create$/).click
      end
      def test_11_EditSettings
        sleep 5
        assert(ie.contains_text($TEXT))
        assert(ie.table(:id,/tblManageSmilies$/).exists?)
        rows = ie.table(:id,/tblManageSmilies$/).row_count
        rows = (rows/2)-1
        smid=rows.to_s
        butid ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+rows.to_s+"_Edit"       
        sleep 5
        ie.button(:id,butid).click
        #update option
        sleep 5
        text_match ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_SmileyCodeEdit"
        text_url ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_SmileyUrlEdit"
        text_desc="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_SmileyTextEdit"
        but_up="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_Update"
        assert(ie.text_field(:id,text_match).exists?)
        ie.text_field(:id,text_match).value = $TextUp
        assert(ie.text_field(:id,text_url).exists?)
        ie.text_field(:id,text_url).value = 'http://www.windweaver.com/emoticon.htm#'+$TextUp
        assert(ie.text_field(:id,text_desc).exists?)
        ie.text_field(:id,text_desc).value = 'Angry_Update '
        assert(ie.button(:id,but_up).exists?)
        ie.button(:id,but_up).click
        #Cancel option
        assert(ie.contains_text($TextUp))
        assert(ie.table(:id,/tblManageSmilies$/).exists?)
        rows = ie.table(:id,/tblManageSmilies$/).row_count
        rows = (rows/2)-1
        smid=rows.to_s
        butid ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+rows.to_s+"_Edit"       
        sleep 5
        ie.button(:id,butid).click
        text_match ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_SmileyCodeEdit"
        text_url ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_SmileyUrlEdit"
        text_desc="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_SmileyTextEdit"
        but_cancel="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+smid+"_Update"
        assert(ie.text_field(:id,text_match).exists?)
        assert(ie.text_field(:id,text_url).exists?)
        assert(ie.button(:id,but_cancel).exists?)
        ie.button(:id,but_cancel).click
        # Delete Option
        assert(ie.contains_text($TextUp))
        assert(ie.table(:id,/tblManageSmilies$/).exists?)
        rows = ie.table(:id,/tblManageSmilies$/).row_count
        rows = (rows/2)-1
        smid=rows.to_s
        butid ="ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_SmiliesList_ctl"+rows.to_s+"_Delete"       
        sleep 5
        ie.button(:id,butid).click
      end
      
      def test_99_closeIE
      ie.close
    end      
      
  end     